# gozerbot/rsslist.py
#
#

""" create a list of rss data """

__copyright__ = 'this file is in the public domain'

import xml.dom.minidom

def gettext(nodelist):
    """ get text data from nodelist """
    result = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            stripped = node.data.strip()
            if stripped:
                result += stripped
    return result

def makersslist(xlist, nodes):
    """ recurse until txt is found """
    tempdict = {}
    for i in nodes:
        txt = gettext(i.childNodes)
        if txt:
            tempdict[i.nodeName] = txt
        else:
            makersslist(xlist, i.childNodes)
    if tempdict:
        xlist.append(tempdict)

def rsslist(txt):
    """ create list of dictionaries with rss data """
    try:
        dom = xml.dom.minidom.parseString(txt)
    except:
        return []
    result = []
    makersslist(result, dom.childNodes)
    return result
